﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.Collections;
using Kugar.Core.ExtMethod;
using Kugar.Core.BaseStruct;

namespace Kugar.UI.WinForm
{
    public partial class TotalColumnRowColumnConfigForm : Form
    {
        public TotalColumnRowColumnConfigForm()
        {
            InitializeComponent();
        }

        public TotalColumnRowColumnConfigForm(DictionaryEx<DataGridViewColumn, TotalColumnConfig> config, DataGridView dg)
            : this()
        {
            Config = config;
            SrcDataGridView = dg;

            foreach (DataGridViewColumn column in dg.Columns)
            {
                var index = dataGridView1.Rows.Add();

                var row = dataGridView1.Rows[index];

                row.Cells[1].Value = column.Name;
                row.Cells[1].Tag = column;

                //MessageBox.Show(row.Cells[0].Value.ToString());

                if (config.ContainsKey(column))
                {
                    var c = config[column];
                    row.Cells[0].Value = true;
                    row.Cells[2].Value = c.FormatString;
                }
            }
        }

        public DictionaryEx<DataGridViewColumn, TotalColumnConfig> Value
        {
            get
            {
                var tempValue = new DictionaryEx<DataGridViewColumn, TotalColumnConfig>();

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if ((bool?)row.Cells[0].Value==true)
                    {
                        var c = new TotalColumnConfig();

                        if (row.Cells[2].Value!=null)
                        {
                            c.FormatString = row.Cells[2].Value.ToStringEx();
                        }

                        tempValue.Add((DataGridViewColumn)row.Cells[1].Tag, c);
                    }
                }

                return tempValue;
            }
        }

        public DictionaryEx<DataGridViewColumn, TotalColumnConfig> Config { private set; get; }

        public DataGridView SrcDataGridView { private set; get; }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
